<?php


    namespace Admin\Event;
    use \Think\Controller;

    class GoodsEvent extends Controller
    {
        //要查询所有商品分类的id 和 name 根据 path 排名
        public function queryAllCate()
        {
            $cate = M("category");
            $list = $cate->field(array("id", "name", "path", "concat(path,id,',') as dpath"))->where("is_show=1")->order("dpath")->select();
            return $list;
        }

        //将商品信息录入到数据库
        Public function insert()
        {
            $data['name'] = I("post.name");
            $data['category_id'] = I("post.cate");
            $data['price'] = I("post.price");
            $data['msg'] = I("post.content");
            $data['goods_type'] = I("post.goods_type");
            $data['create_time'] = date("Y-m-d H:i:s");
            //上传图片,把图片名放入shopping_goodsimg
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = './Uploads/'; // 设置附件上传根目录
            $upload->savePath = 'GoodsImg/'; // 设置附件上传（子）目录
            // 上传文件
            $info = $upload->upload();

            if (!$info) {// 上传错误提示错误信息
                return $upload->getError();
                die;
            }
            $basename = $info['photo']['savename'];
            $basepath = $info['photo']['savepath'];
            $admin = A("Admin", "Controller");
            //图片的路径
            $ImgName = $admin->get_img_url("./Uploads/GoodsImg/", $basename, 0);
            //把商品图片进行缩略图显示

            $image = new \Think\Image();
            $image->open($ImgName);
            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            //生成保存路径
            $saveImg = "./Uploads/" . $basepath;
            //进行缩放
            $_350 = $image->thumb(350, 350)->save($saveImg . "350_" . $basename);
            $_250 = $image->thumb(250, 250)->save($saveImg . "250_" . $basename);
            $_150 = $image->thumb(150, 150)->save($saveImg . "150_" . $basename);
            $_50 = $image->thumb(50, 50)->save($saveImg . "50_" . $basename);
            //如果有一张缩放失败,就把原图和缩放成功的图片都删掉
            if (!$_350 || !$_250 || !$_150 || !$_50) {
                @unlink($ImgName);
                @unlink($saveImg . "350_" . $basename);
                @unlink($saveImg . "250_" . $basename);
                @unlink($saveImg . "150_" . $basename);
                @unlink($saveImg . "50_" . $basename);
                return "图片缩放失败";
                die;
            }
            //把商品信息添加到数据库
            $goods = M("goods");
            $id = $goods->add($data);//返回自增id
            if ($id <= 0) {
                //把原图和缩放成功的图片都删掉
                @unlink($ImgName);
                @unlink($saveImg . "350_" . $basename);
                @unlink($saveImg . "250_" . $basename);
                @unlink($saveImg . "150_" . $basename);
                @unlink($saveImg . "50_" . $basename);
                return "商品信息添加到数据库失败";
                die;
            }
            //把商品图片添加到数据库
            $goodsImg = M("goodsimg");
            $imgData['img'] = $basename;
            $imgData['goods_id'] = $id;
            $imgData['is_main'] = 1;
            $imgData['create_time'] = date("Y-m-d H:i:s");
            $img_id = $goodsImg->add($imgData);
            if ($img_id <= 0) {
                //把添加成功的商品信息删除
                $goods->where("id=$id")->delete();
                //把原图和缩放成功的图片都删掉
                @unlink($ImgName);
                @unlink($saveImg . "350_" . $basename);
                @unlink($saveImg . "250_" . $basename);
                @unlink($saveImg . "150_" . $basename);
                @unlink($saveImg . "50_" . $basename);
                return "商品图片添加到数据库失败";
                die;
            }
            return "添加成功";


        }

        //查询所有商品
        public function queryGoods()
        {
            $goods = M("goods");
            $count = $goods -> count("id");//查出总记录数
            $page = new \Think\Page($count,5);//每页显示  5 条数据
            //设置分页属性
            $show = $page -> show(); //分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $goodslist = $goods
                ->alias("g")->join("shopping_category c on g.category_id = c.id")
                ->join("shopping_goodsimg i on g.id = i.goods_id")
                ->where("i.is_main=1")
                ->field(array("g.id", "g.name", "g.price", "g.num", "g.sales_num", "g.is_show", "g.is_new", "g.is_hot","g.is_integral", "g.is_coupon", "g.special_id", "g.seckill_id", "g.integral_price", "c.name cname", "i.img"))
                ->limit($page->firstRow.",".$page->listRows) ->select();
            $data['show'] = $show;
            $data['list'] = $goodslist;

            return $data;
        }
        //商品上架切换
        public function change_show ()
        {
            //如果上架就改为下架   0为下架,  1位上架
            if(I("post.is_show") == 1){
                $data["is_show"] = 0;
            }else{
                $data["is_show"] = 1;
            }
            $id = I("post.id");

            $goods = M("goods");
            $rows = $goods -> where("id=$id")->save($data);
            if($rows > 0){
                return $data["is_show"];
            }else{
                return -1;
            }
        }
        //商品热销切换
        public function change_hot ()
        {
            //如果热销就改为不热销  0为不热销,  1位热销
            if(I("get.is_hot") == 1){
                $data["is_hot"] = 0;
            }else{
                $data["is_hot"] = 1;
            }
            $id = I("get.id");

            $goods = M("goods");
            $rows = $goods -> where("id=$id")->save($data);
            if($rows > 0){
                return $data["is_hot"];
            }else{
                return -1;
            }
        }
        //商品新品切换
        public function change_new ()
        {
            //如果新品就改为不是新品   0为不是新品,  1位新品
            if(I("get.is_new") == 1){
                $data["is_new"] = 0;
            }else{
                $data["is_new"] = 1;
            }
            $id = I("get.id");

            $goods = M("goods");
            $rows = $goods -> where("id=$id")->save($data);
            if($rows > 0){
                return $data["is_new"];
            }else{
                return -1;
            }
        }

        //商品信息详情查看,根据id查询
        public function look()
        {
           $id = I("get.id");
           $goods = M("goods");
           $goodsinfo = $goods
                ->alias("g")->join("shopping_category c on g.category_id = c.id")
                ->join("shopping_goodsimg i on g.id = i.goods_id")
                ->field(array("g.id", "g.name", "g.price", "g.num", "g.sales_num", "g.is_show", "g.is_new", "g.is_hot", "g.msg", "g.is_integral", "g.is_coupon", "g.special_id", "g.seckill_id", "g.integral_price", "g.update_time", "g.create_time", "c.name cname", "i.img","g.category_id","g.goods_type"))
                ->where("g.id=$id")
                ->find();

           return $goodsinfo;
        }

        //商品信息编辑提交处理
        public function editDo ()
        {

            $id = I("post.id");
            $data['name'] = I("post.name");//商品名字
            $data['category_id'] = I("post.cate");//商品分类
            $data['price'] = I("post.price");//价格
            $data['num'] = I("post.num");//库存
            $data['goods_type'] = I("post.goods_type");//商品类别
            $data['is_integral'] = I("post.is_integral");//是否开启积分购买
            $data['integral_price'] = I("post.integral_price");//需要购买此商品的积分数量
            $data['is_coupon'] = I("post.is_coupon");//是否可以使用优惠券购买
            $data['special_id'] = I("post.special_id");//是否参加专题
            $data['seckill_id'] = I("post.seckill_id");//是否参加秒杀
            $data['msg'] = I("post.msg");//商品详情

            $goods = M("goods");
            $row = $goods -> where("id=$id") -> save($data);
            return $row;
        }

        //商品图片编辑,根据id查询
        public function editImg ()
        {
            $goods_id = I("get.id");
            $img = M("goodsimg");
            $goodsImgList = $img ->order("is_main desc") ->where("goods_id=$goods_id") -> select();
            return $goodsImgList;
        }

        //给商品添加图片
        public function addImg ()
        {

                $id = I("post.goods_id");

                //上传图片,把图片名放入shopping_goodsimg
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Uploads/'; // 设置附件上传根目录
                $upload->savePath = 'GoodsImg/'; // 设置附件上传（子）目录
                // 上传文件
                $info = $upload->upload();

                if (!$info) {// 上传错误提示错误信息
                    return $upload->getError();
                    die;
                }
                //获得添加图片的文件名
                $basename = $info['photo']['savename'];
                //获得图片的路径
                $basepath = $info['photo']['savepath'];
                //实例化控制器
                $admin = A("Admin", "Controller");
                //图片的路径
                $ImgName = $admin->get_img_url("./Uploads/GoodsImg/", $basename, 0);
                //把商品图片进行缩略图显示

                $image = new \Think\Image();
                $image->open($ImgName);
                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                //生成保存路径
                $saveImg = "./Uploads/" . $basepath;
                //进行缩放
                $_350 = $image->thumb(350, 350)->save($saveImg . "350_" . $basename);
                $_250 = $image->thumb(250, 250)->save($saveImg . "250_" . $basename);
                $_150 = $image->thumb(150, 150)->save($saveImg . "150_" . $basename);
                $_50 = $image->thumb(50, 50)->save($saveImg . "50_" . $basename);
                //如果有一张缩放失败,就把原图和缩放成功的图片都删掉
                if (!$_350 || !$_250 || !$_150 || !$_50) {
                    @unlink($ImgName);
                    @unlink($saveImg . "350_" . $basename);
                    @unlink($saveImg . "250_" . $basename);
                    @unlink($saveImg . "150_" . $basename);
                    @unlink($saveImg . "50_" . $basename);
                    return "图片缩放失败";
                    die;
                }
                //把商品图片添加到数据库
                $goodsImg = M("goodsimg");
                $imgData['img'] = $basename;
                $imgData['goods_id'] = $id;
                $imgData['create_time'] = date("Y-m-d H:i:s");
                $img_id = $goodsImg->add($imgData);
                if ($img_id <= 0) {
                    //把原图和缩放成功的图片都删掉
                    @unlink($ImgName);
                    @unlink($saveImg . "350_" . $basename);
                    @unlink($saveImg . "250_" . $basename);
                    @unlink($saveImg . "150_" . $basename);
                    @unlink($saveImg . "50_" . $basename);
                    return "商品图片添加到数据库失败";
                    die;
                }
                return "添加成功";
            }

        //给商品设置封面图
        public function setMain ()
        {
            $id = I("get.id");
            $goods_id = I("get.goods_id");
            //要想设封面图,必须先把是封面图的的图片的is_main 改为0
            $goodsImg = M("goodsimg");
            $data['is_main'] = 0;
            $row = $goodsImg -> where("goods_id=$goods_id and is_main=1") ->save($data);
            if($row > 0){
                $data['is_main'] = 1;
                $rows = $goodsImg -> where("goods_id=$goods_id and id=$id") ->save($data);
                return $rows;
            }else{
                return $row;
            }
        }

        //前台页面的商品图片显示设置
        public function setShow ()
        {
            $id = I("get.id");
            $goods_id = I("get.goods_id");
            $is_show = I("get.is_show");
            if($is_show == 0){
                $data['is_show'] = 1;
            }else{
                $data['is_show'] = 0;
            }
            $goodsImg = M("goodsimg");
            $row = $goodsImg -> where("goods_id=$goods_id and id=$id") ->save($data);
            return $row;
        }


        //商品图片删除
        public function delImg ()
        {
            $id = I("get.id");
            $goods_id = I("get.goods_id");
            $goodsImg = M("goodsimg");
            $row = $goodsImg -> where("goods_id=$goods_id and id=$id") ->delete();
            return $row;
        }














    }


